package com.yeyuanqi.untils;

import com.yeyuanqi.web.client.CommentController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Component
public class PythonUntils {
    private static final String biblibilipath = "E:\\IDEAworkspace\\blog_system\\src\\main\\resources\\python\\BiSheTengxunxinwenTest.py";
    private static final String baidupath = "E:\\IDEAworkspace\\blog_system\\src\\main\\resources\\python\\bishebaidutest.py";
    private static final String weibopath = "E:\\IDEAworkspace\\blog_system\\src\\main\\resources\\python\\BiSheWeiBoTest.py";
    private static final String jinritoutiaopath = "E:\\IDEAworkspace\\blog_system\\src\\main\\resources\\python\\bishejinritoutiaotest.py";

    private static final Logger logger = LoggerFactory.getLogger(PythonUntils.class);
    public void starttengxunxinwen() {
        Process proc;
        logger.info("启动腾讯新闻脚本");
        try {
            proc = Runtime.getRuntime().exec("python " + biblibilipath);// 执行py文件
            //用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void startweibo() {
        Process proc;
        logger.info("启动微博脚本");
        try {
            proc = Runtime.getRuntime().exec("python " + weibopath);// 执行py文件
            //用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void startbaidu() {
        Process proc;
        logger.info("启动百度脚本");
        try {
            proc = Runtime.getRuntime().exec("python " + baidupath);// 执行py文件
//            用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void startjinritoutiao() {
        Process proc;
        logger.info("启动今日头条脚本");
        try {
            proc = Runtime.getRuntime().exec("python " + jinritoutiaopath);// 执行py文件
//            用输入输出流来截取结果
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"utf8"));
            String line = null;
            while ((line = in.readLine()) != null) {
                System.out.println(line);
            }
            in.close();
            proc.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
